package fatecpg.com.br.japassei;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

import fatecpg.com.br.japassei.bd.Disciplina;
import fatecpg.com.br.japassei.bd.DisciplinaDAO;


public class ListaActivity extends Activity {
    ArrayList<Disciplina> disciplinas = new ArrayList<Disciplina>();
    ArrayList<String> lista = new ArrayList<String>();
    DisciplinaDAO disciplinaDAO;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lista);
        disciplinaDAO = new DisciplinaDAO(this.getApplicationContext());
        inicializar();
    }

    public void informar(View view) {
        Intent intent = new Intent(this, SobreActivity.class);
        startActivity(intent);
    }

    public void configurar(View view) {
        Intent intent = new Intent(this, ConfigActivity.class);
        startActivity(intent);
    }

    public void nova(View view) {
        Intent intent = new Intent(this, DisciplinaActivity.class);
        intent.putExtra("ic_novo", true);
        intent.putExtra("ic_editar", false);
        startActivity(intent);
    }

    public void inicializar() {
        disciplinas.clear();
        ListView lstDisciplinas = (ListView) findViewById(R.id.lstDisciplinas);
        this.lista = getItens();
        CharSequence[] cs = this.lista.toArray(new CharSequence[lista.size()]);
        ListAdapter adaptador = new Adaptador().adaptarUnicoIcone(getApplicationContext(), cs, R.drawable.ic_livro);
        lstDisciplinas.setAdapter(adaptador);
        lstDisciplinas.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        lstDisciplinas.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Intent notas = new Intent(ListaActivity.this, MainActivity.class);

                try {
                    notas.putExtra("qt_prova1", disciplinas.get(position).getNota().getNota_p1());
                }catch(NullPointerException ex){
                    notas.putExtra("qt_prova1", 0.00);
                }
                try {
                    notas.putExtra("qt_prova2", disciplinas.get(position).getNota().getNota_p2());
                }catch(NullPointerException ex){
                    notas.putExtra("qt_prova2", 0.00);
                }
                try {
                    notas.putExtra("qt_traba", disciplinas.get(position).getNota().getNota_tp());
                }catch (NullPointerException ex){
                    notas.putExtra("qt_traba", 0.00);
                }

                //notas.putExtra("qt_prova1", 8.00);

                // notas.putExtra("qt_prova2", 9.00);

                //  notas.putExtra("qt_traba", 8.00);

                notas.putExtra("cd_disciplina", Utilidades.nvl(disciplinas.get(position).getCdDisciplina()));
                notas.putExtra("ic_editar", true);
                notas.putExtra("ic_novo", false);
                startActivity(notas);
            }
        });
        lstDisciplinas.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView<?> adaptador, View view, final int position, long id) {
                final CharSequence[] items = {
                        "Ver",
                        "Editar",
                        "Notas",
                        "Excluir",
                        "Cancelar"
                };


                final int[] icons = {
                        R.drawable.ic_visualizar,
                        R.drawable.ic_editar,
                        R.drawable.ic_calc,
                        R.drawable.ic_excluir,
                        R.drawable.ic_cancelar
                };
                ListAdapter adaptadorMenu = new Adaptador().adaptar(getApplicationContext(), items, icons);

                AlertDialog.Builder builder = new AlertDialog.Builder(ListaActivity.this);
                builder.setTitle("Opções");

                builder.setAdapter(adaptadorMenu, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int item) {
                        switch (item) {
                            case 0:
                                Intent ver = new Intent(ListaActivity.this, DisciplinaActivity.class);
                                ver.putExtra("nm_disciplina", disciplinas.get(position).getNmDisciplina());
                                ver.putExtra("ds_disciplina", disciplinas.get(position).getDsDisciplina());
                                ver.putExtra("cd_disciplina", String.valueOf(disciplinas.get(position).getCdDisciplina()));
                                ver.putExtra("ic_editar", false);
                                ver.putExtra("ic_novo", false);
                                ver.putExtra("ic_ver", true);
                                startActivity(ver);
                                break;
                            case 1:
                                Intent editar = new Intent(ListaActivity.this, DisciplinaActivity.class);
                                editar.putExtra("nm_disciplina", disciplinas.get(position).getNmDisciplina());
                                editar.putExtra("ds_disciplina", disciplinas.get(position).getDsDisciplina());
                                editar.putExtra("cd_disciplina", String.valueOf(disciplinas.get(position).getCdDisciplina()));
                                editar.putExtra("ic_editar", true);
                                editar.putExtra("ic_novo", false);
                                startActivity(editar);
                                break;
                            case 2:
                                Intent notas = new Intent(ListaActivity.this, MainActivity.class);

                                try {
                                    notas.putExtra("qt_prova1", disciplinas.get(position).getNota().getNota_p1());
                                }catch(NullPointerException ex){
                                    notas.putExtra("qt_prova1", 0.00);
                                }
                                try {
                                    notas.putExtra("qt_prova2", disciplinas.get(position).getNota().getNota_p2());
                                }catch(NullPointerException ex){
                                    notas.putExtra("qt_prova2", 0.00);
                                }
                                try {
                                    notas.putExtra("qt_traba", disciplinas.get(position).getNota().getNota_tp());
                                }catch (NullPointerException ex){
                                    notas.putExtra("qt_traba", 0.00);
                                }

                                //notas.putExtra("qt_prova1", 8.00);

                               // notas.putExtra("qt_prova2", 9.00);

                              //  notas.putExtra("qt_traba", 8.00);

                                notas.putExtra("cd_disciplina", Utilidades.nvl(disciplinas.get(position).getCdDisciplina()));
                                notas.putExtra("ic_editar", true);
                                notas.putExtra("ic_novo", false);
                                startActivity(notas);
                                break;
                            case 3:
                                if (disciplinaDAO.delDisciplina(disciplinas.get(position).getCdDisciplina())) {
                                    Toast.makeText(ListaActivity.this.getApplicationContext(), "Disciplina deletada", Toast.LENGTH_SHORT).show();
                                    disciplinas.clear();
                                    disciplinas = disciplinaDAO.getDisciplinas();
                                } else {
                                    Toast.makeText(ListaActivity.this.getApplicationContext(), "Erro ao deletar disciplina", Toast.LENGTH_SHORT).show();
                                }

                                inicializar();
                                break;
                            default:
                                Toast.makeText(ListaActivity.this.getApplicationContext(), "Nada selecionado", Toast.LENGTH_SHORT).show();
                                break;
                        }
                    }
                });
                builder.setIcon(R.drawable.ic_pin);
                AlertDialog alert = builder.create();
                alert.show();
                return true;
            }
        });

        //Toast.makeText(getApplicationContext(), "Lista carregada", Toast.LENGTH_SHORT).show();
    }

    public void calcular(View view) {
        Intent notas = new Intent(ListaActivity.this, MainActivity.class);
        notas.putExtra("ic_editar", false);
        startActivity(notas);
    }

    public ArrayList<String> getItens() {
        this.disciplinas.clear();
        this.lista.clear();
        this.disciplinas = disciplinaDAO.getDisciplinas();
        for (Disciplina d : disciplinas) {
            this.lista.add(d.getNmDisciplina());
        }
        return this.lista;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_lista, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void onResume() {
        super.onResume();
        inicializar();
    }


}